package com.atguigu.spring6.aop.xmlaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

     // 前置通知
    public void beforeMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger-->前置通知，方法是：" + name);
    }

    // 后置通知
    public void afterMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("Logger-->后置通知，方法是：" + name + "方法执行参数是：" + args.toString());
    }

    // 返回通知
    public void afterReturningMethod(JoinPoint joinPoint, Object result) {
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger-->返回通知，结果是：" + name + "方法执行结果是：" + result);
    }

    // 异常通知

    public void afterThrowingMethod(JoinPoint joinPoint, Throwable e) {
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger-->异常通知，方法是：" + name + "异常信息：" + e.getMessage());
    }

    // 环绕通知
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            System.out.println("环绕通知-->前置");
            // 被增强的方法执行
            result = joinPoint.proceed();
            System.out.println("环绕通知-->后置");
        } catch (Throwable e) {
            System.out.println("环绕通知-->异常信息" + e.getMessage());
        } finally {
            System.out.println("环绕通知-->最终");
        }

        return result;
    }

}
